package cn.king.product.service.impl;

import cn.king.basic.service.impl.BaiduAiAuditServiceImpl;
import cn.king.basic.util.AjaxResult;
import cn.king.product.domain.Product;
import cn.king.product.domain.ProductDetail;
import cn.king.product.domain.ProductOnlineAuditLog;
import cn.king.product.mapper.ProductDetailMapper;
import cn.king.product.mapper.ProductMapper;
import cn.king.product.mapper.ProductOnlineAuditLogMapper;
import cn.king.product.service.IProductService;
import cn.king.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;
    @Autowired
    private BaiduAiAuditServiceImpl baiduAiAuditService;
    @Autowired
    private ProductOnlineAuditLogMapper productOnlineAuditLogMapper;

    @Transactional
    @Override
    public void add(Product product) {
        super.add(product);
        //添加宠物详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.add(productDetail);
        }
    }
    @Transactional
    @Override
    public void update(Product product) {
        super.update(product);
        //修改宠物详情
        ProductDetail productDetail = product.getProductDetail();
        if (productDetail != null){
            productDetail.setProductId(product.getId());
            productDetailMapper.updateByProductId(productDetail);
        }
    }
    @Transactional
    @Override
    public void delete(Long id) {
        //删除宠物详情
        productDetailMapper.delByProductId(id);
        //删除宠物信息
        super.delete(id);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        super.patchDelete(ids);
        for (Long id : ids) {
            delete(id);
        }
    }

    /**
     * 上架
     * @param ids
     */
    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id : ids) {
            Product product = productMapper.getOne(id);
            //审核内容和图像
            Boolean boo1 = baiduAiAuditService.textAudit(product.getName());
            Boolean boo2 = baiduAiAuditService.imageAudit(product.getResources());
            ProductOnlineAuditLog log = new ProductOnlineAuditLog();
            log.setProductId(id);
            if (boo1 && boo2) {
                //改状态
                productMapper.onsale(id, new Date());
                //添加审核日志
                log.setState(1);//审核成功
                log.setNote("审核成功");
                productOnlineAuditLogMapper.add(log);
            } else {
                //添加审核日志
                log.setState(0);//审核失败
                log.setNote("审核失败");
                productOnlineAuditLogMapper.add(log);
                return AjaxResult.me().setMsg("审核失败").setSuccess(false);
            }
        }
        return new AjaxResult();
    }

    /**
     * 下架
     * @param ids
     */
    @Override
    public AjaxResult offsale(List<Long> ids) {
        //改状态 //方式1：先查再改   //方式2：直接改需要写sql
        //Mybatis多参数操作：1.Map 2.对象 3.索引下标#{0} 4.注解
        productMapper.offsale(ids,new Date());
        return new AjaxResult();
    }
}
